package com.td.module.equipment.framework.core.callbak;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.td.module.equipment.dal.dataobject.info.InfoDO;
import com.td.module.equipment.framework.core.AbsMqttCallBack;
import com.td.module.equipment.framework.core.dto.EquipmentDataDto;
import com.td.module.equipment.framework.core.dto.EquipmentInfoBaseDto;
import com.td.module.equipment.framework.core.dto.EquipmentInfoCreateDto;
import com.td.module.equipment.framework.core.dto.EquipmentInfoUpdateDto;
import com.td.module.equipment.service.acquisitioninfo.AcquisitionInfoService;
import com.td.module.equipment.service.info.InfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhanghe
 * @date 2023/4/16
 */
@Slf4j
@Component("9a6f27696d6455da06b09a9c51da35a4")
public class DtMqttCallBack extends AbsMqttCallBack {

    @Resource
    private AcquisitionInfoService acquisitionInfoService;

    @Resource(name = "equipmentInfoService")
    private InfoService infoService;


    @Override
    protected void handleMessageArrived(String topic, String message) {

        try {

            String topicType = topic.split("/")[3];
            JSONObject payLoadJsonObj = JSONUtil.parseObj(message);
            String cmd = payLoadJsonObj.getStr("cmd");

            // 根据报文中的话题判断业务类型
            switch (topicType) {

                case "offline": {

                    InfoDO infoDO = infoService.getInfo(payLoadJsonObj.getJSONObject("ext").getStr("imei"));
                    if (infoDO == null) {

                        EquipmentInfoCreateDto equipmentInfoCreateDto = new EquipmentInfoCreateDto();
                        EquipmentInfoBaseDto.Ext ext = new EquipmentInfoBaseDto.Ext();
                        ext.setStationID(payLoadJsonObj.getJSONObject("ext").getStr("imei"));
                        equipmentInfoCreateDto.setExt(ext);

                        if ("offline".equals(cmd)) {
                            equipmentInfoCreateDto.setIsOnline(0);
                        } else {
                            equipmentInfoCreateDto.setIsOnline(1);
                        }

                        infoService.createInfo(equipmentInfoCreateDto);

                    } else {

                        EquipmentInfoUpdateDto equipmentInfoUpdateDto = new EquipmentInfoUpdateDto();
                        EquipmentInfoUpdateDto.Ext ext = new EquipmentInfoUpdateDto.Ext();
                        ext.setStationID(infoDO.getStationId());
                        equipmentInfoUpdateDto.setId(infoDO.getId());
                        equipmentInfoUpdateDto.setExt(ext);

                        if ("offline".equals(cmd)) {
                            equipmentInfoUpdateDto.setIsOnline(0);
                        } else {
                            equipmentInfoUpdateDto.setIsOnline(1);
                        }

                        infoService.updateInfo(equipmentInfoUpdateDto);
                    }

                    break;
                }

                case "pub": {

                    if ("terminalData".equals(cmd)) {
                        EquipmentDataDto equipmentDataDto = JSONUtil.toBean(message, EquipmentDataDto.class);
                        acquisitionInfoService.createAcquisitionInfo(equipmentDataDto);
                    } else if ("Status".equals(cmd)) {

                        InfoDO infoDO = infoService.getInfo(payLoadJsonObj.getJSONObject("ext").getStr("StationID"));

                        if (infoDO == null) {

                            EquipmentInfoCreateDto equipmentInfoCreateDto = JSONUtil.toBean(message, EquipmentInfoCreateDto.class);
                            infoService.createInfo(equipmentInfoCreateDto);

                        } else {

                            EquipmentInfoUpdateDto equipmentInfoUpdateDto = JSONUtil.toBean(message, EquipmentInfoUpdateDto.class);
                            equipmentInfoUpdateDto.setId(infoDO.getId());

                            infoService.updateInfo(equipmentInfoUpdateDto);

                        }
                    }
                    break;

                }

                default:
                    log.info("[设备信息话题异常]", topic);
                    break;

            }


        } catch (Throwable ex) {
            // 打印异常日志
            log.error("[设备采集信息保存异常]", ex);
        }

    }
}
